
#ifndef DUMPOBJECTTREE_H
#define	DUMPOBJECTTREE_H

#include <QObject>
#include <QWidget>
#include "Node.h"

class DumpTree : public QObject {

    Q_OBJECT

public:
    static void dumpObjectTree(const QObject* ob) {
        dumpRecursive(0, ob);
    }

    static void dumpNodeTree(const QObject* ob) {
        qDebug("=============== objects =================");
        dumpRecursiveNode(0, ob);
    }

private:

    static void dumpRecursive(int level, const QObject *object) {
#if defined(QT_DEBUG)
        if (object) {
            QByteArray buf;
            buf.fill(' ', level / 2 * 8);
            if (level % 2)
                buf += "    ";
            QString name = object->objectName();
            QString flags = QLatin1String("");
#if 1
            //            if (qApp->focusWidget() == object)
            //                flags += 'F';
            if (object->isWidgetType()) {
                QWidget * w = (QWidget *) object;
                if (w->isVisible()) {
                    QString t("<%1,%2,%3,%4>");
                    flags += t.arg(w->x()).arg(w->y()).arg(w->width()).arg(w->height());
                } else {
                    flags += 'I';
                }
            }
#endif
            qDebug("%s%s::%s %s", (const char*) buf, object->metaObject()->className(), name.toLocal8Bit().data(),
                    flags.toLatin1().data());
            QObjectList children = object->children();
            if (!children.isEmpty()) {
                for (int i = 0; i < children.size(); ++i)
                    dumpRecursive(level + 1, children.at(i));
            }
        }
#else
        Q_UNUSED(level)
        Q_UNUSED(object)
#endif
    }

    static void dumpRecursiveNode(int level, const QObject *object) {

        if (object) {
            const Node* node = qobject_cast<const Node *>(object);
            if (node) {
                if (node->getNodeType() == "top") qDebug(" ");

                QByteArray buf;
                buf.fill(' ', level * 4);

                QString eq = QLatin1String("");
                std::vector<Node*> v = node->getEquals();
                for (auto it = v.begin(); it != v.end(); ++it)
                    eq.append("~").append((*it)->toCasString());
                if (v.size() == 0 ) eq = "none";

                qDebug("%s%s::%s  c:%s  pi:%s pr:%s  e:%s",
                        (const char*) buf,
                        node->metaObject()->className(),
                        node->objectName().toLocal8Bit().data(),
                        node->toCasString().toLatin1().data(),
                        node->toPrintString().toLatin1().data(),
                        node->toParseString().toLatin1().data(),
                        eq.toLatin1().data()
                        );
            }

            QObjectList children = object->children();
            if (!children.isEmpty()) {
                for (int i = 0; i < children.size(); ++i)
                    dumpRecursiveNode(level + 1, children.at(i));
            }
        }
    }

};


#endif	/* DUMPOBJECTTREE_H */

